package com.qd.admin.controller.article;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.SpringContextHolder;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.sys.domain.entity.article.ArticleDTO;
import com.qd.common.sys.event.ArticleHintCountEvent;
import com.qd.system.service.article.ArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 内容发布
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/article")
public class AppArticleController {

    private final ArticleService articleService;
    /**
     * 内容详情
     */
    @GetMapping(value = "/getById")
    public ArticleDTO getById(@Validated @NotBlank(message = "文章ID不能为空") String articleId, Integer type) {
        final ArticleDTO articleDto = articleService.getByIdDto(articleId);
        if (null == articleDto) {
            throw new ApiException("内容不存在[" + articleId + "]");
        }
        //个人用户查看才统计数量
        if (null != type && type == 1) {
            SpringContextHolder.publishEvent(new ArticleHintCountEvent(articleId));
        }
        return articleDto;
    }

    /**
     * 内容列表
     */
    @GetMapping(value = "/page")
    public IPage<ArticleDTO> page(ArticleDTO articleDto) {
        if (null == articleDto) {
            articleDto = new ArticleDTO();
        }
        return articleService.appPage(PageUtil.buildPage(articleDto), articleDto);
    }
}